package com.vr.xuecheng.common.util;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author hzh
 * @date 2025/03/31
 * 获取当前的用户信息
 */
@Log4j2
public class SecurityUtils {
    public static User getUser() {
        try {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (principal instanceof String) {
                String json = principal.toString();
                return JSON.parseObject(json, User.class);
            }
        } catch (Exception e) {
            log.error("获取当前用户信息失败:{}", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Data
    public static class User implements Serializable {

        @Serial
        private static final long serialVersionUID = 1L;

        private String id;

        private String username;

        private String password;

        private String salt;

        private String name;
        private String nickname;
        private String wxUnionid;
        private String companyId;
        /**
         * 头像
         */
        private String userpic;

        private String utype;

        private LocalDateTime birthday;

        private String sex;

        private String email;

        private String cellphone;

        private String qq;

        /**
         * 用户状态
         */
        private String status;

        private LocalDateTime createTime;

        private LocalDateTime updateTime;

    }

}
